//
//  FindViewController.m
//  DefendHealth
//
//  Created by UNOHACHA on 17/2/18.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "FindViewController.h"
#import "SGTopTitleView.h"
#import "ConsultingViewController.h"
#import "ActivityViewController.h"
@interface FindViewController ()<SGTopTitleViewDelegate,UIScrollViewDelegate,UITextFieldDelegate>
{
    UIImageView *searchImg;
    UIButton *releaseBtn;
}
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic,strong) ConsultingViewController *arDyVC ;
@property (nonatomic, strong) SGTopTitleView *topTitleView;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     self.TitleNav.hidden = YES;
    self.leftbutton.hidden = YES;
    self.titles = @[@"咨询", @"活动"];
    //1. 创建滑动View
    [self topTitiViewInit];
    //2.添加所有子控制器
    [self setupChildViewController];
    //3.初始化scrollview
    [self.view addSubview:self.mainScrollView];
    
    
    [_mainScrollView addSubview:_arDyVC.view];
    [self.view insertSubview:_mainScrollView belowSubview:_topTitleView];
    
    searchImg = [UIImageView new];
    searchImg.frame = CGRectMake(WindowWidth-10-17, 30, 17, 19);
    searchImg.image = [UIImage imageNamed:@"search"];
    [self.view addSubview:searchImg];
    
    
    releaseBtn = [[UIButton alloc] initWithFrame:CGRectMake(WindowWidth-10-[ToolUtil autoWidth:@"我参与的" width:500 height:13 num:13], 20, [ToolUtil autoWidth:@"我参与的" width:500 height:13 num:13], 40)];
    [releaseBtn setTitle:@"我参与的" forState:UIControlStateNormal];
//    [releaseBtn addTarget:self action:@selector(release:) forControlEvents:UIControlEventTouchUpInside];
    releaseBtn.titleLabel.font = FONT(13);
    releaseBtn.titleLabel.textAlignment = 2;
    [releaseBtn setTitleColor:[ImageUtil colorWithRgb:0x333333] forState:0];
    releaseBtn.hidden = YES;
    [self.view addSubview:releaseBtn];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        // 创建底部滚动视图
        _mainScrollView = [[UIScrollView alloc] init];
        _mainScrollView.frame = CGRectMake(0, kMaxY(_topTitleView.frame),kScreenWidth, kScreenHeight-kMaxY(_topTitleView.frame));
        _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
        _mainScrollView.backgroundColor = [UIColor clearColor];
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.bounces = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.delegate = self;
        _mainScrollView.scrollEnabled = NO;
        
    }
    return _mainScrollView;
}

-  (void)topTitiViewInit {
    
    self.topTitleView = [SGTopTitleView topTitleViewWithFrame:CGRectMake((WindowWidth-100)/2, 20, 100, 44)];
    _topTitleView.staticTitleArr = [NSArray arrayWithArray:_titles];
    _topTitleView.delegate_SG = self;
    _topTitleView.layer.shadowColor=[HOME_COLOR colorWithAlphaComponent:0.8].CGColor;
    _topTitleView.layer.shadowOffset=CGSizeMake(0,40);
    
    _topTitleView.layer.shadowOpacity=0.5;
    _topTitleView.backgroundColor = [UIColor whiteColor];
    _topTitleView.layer.shadowRadius=8;
    [self.view addSubview:_topTitleView];
}

#pragma mark - - - SGTopScrollMenu代理方法
- (void)SGTopTitleView:(SGTopTitleView *)topTitleView didSelectTitleAtIndex:(NSInteger)index{
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * kScreenWidth;
    _mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
    if (index == 0) {
        searchImg.hidden = NO;
        releaseBtn.hidden = YES;
    }
    else{
        searchImg.hidden = YES;
        releaseBtn.hidden = NO;
    }
}
#pragma mark - -  显示控制器的view
- (void)showVc:(NSInteger)index {
    UIViewController *vc = self.childViewControllers[index];
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    
    [_mainScrollView addSubview:vc.view];
    vc.view.x = index * kScreenWidth;
}

#pragma mark - -   添加所有子控制器

- (void)setupChildViewController {
    // 咨询
    ConsultingViewController *arDyVC = [[ConsultingViewController alloc] init];
    _arDyVC = arDyVC;
    [self addChildViewController:arDyVC];
    
    // 活动
    ActivityViewController *arSuVC = [[ActivityViewController alloc] init];
    [self addChildViewController:arSuVC];
    
}
@end
